﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using WebApi.Helpers;
using WebApi.Model;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CheckDeviceController : BaseController
    {
        public CheckDeviceController(ILogger<CheckDeviceController> logger):base(logger)
        {
        }

        /// <summary>
        /// 获取维护数据分页列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("GetPageList")]
        public async Task<ReturnModel> GetPageList(PageQueryBase model)
        {
            try
            {
                Expression<Func<CheckDevice, bool>> filter = e => true;
                if (model.BeginTime != null)
                    filter = filter.And(c => Convert.ToDateTime(c.Date) >= model.BeginTime);
                if (model.EndTime != null)
                    filter = filter.And(c => Convert.ToDateTime(c.Date) <= model.EndTime);
                SqlSugar.PageModel pageModel = new SqlSugar.PageModel { PageIndex = model.PageIndex, PageSize = model.PageSize };
                var data = await DbHelper.CheckDevice.GetPageListAsync(filter, pageModel);
                return new ReturnModel { Count = pageModel.TotalCount, Data = data };
            }
            catch (Exception ex)
            {
                LogToDB("CheckDevice", "查询", $"参数：{JsonConvert.SerializeObject(model)}", "异常", $"{nameof(GetPageList)}-{ex.Message}");
                return logger.ErrorModel($"{nameof(MonitorController)}/{nameof(GetPageList)}", ex);
            }
        }

        /// <summary>
        /// 获取维护数据列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("GetList")]
        public async Task<ReturnModel> GetList(PageQueryBase model)
        {
            try
            {
                Expression<Func<CheckDevice, bool>> filter = e => true;
                if (model.BeginTime != null)
                    filter = filter.And(c => Convert.ToDateTime(c.Date) >= model.BeginTime);
                if (model.EndTime != null)
                    filter = filter.And(c => Convert.ToDateTime(c.Date) <= model.EndTime);
                var data = await DbHelper.CheckDevice.GetListAsync(filter);
                return new ReturnModel { Count = data.Count, Data = data };
            }
            catch (Exception ex)
            {
                LogToDB("CheckDevice", "查询", $"参数：{JsonConvert.SerializeObject(model)}", "异常", $"{nameof(GetList)}-{ex.Message}");
                return logger.ErrorModel($"{nameof(MonitorController)}/{nameof(GetList)}", ex);
            }
        }
    }
}
